if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}
clear all
set more off
set maxvar 10000


*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1
drop if replacement == 1 & gender == 1

*Define strata
egen strata=group(gender state_old)

ren edu_level_b  edu_b

*Generate treatment streams
gen treatment1=0
replace treatment1=1 if treatment_stream==4
gen treatment2=0
replace treatment2=1 if treatment_stream==3 

*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate interaction for other geographic controls
replace num_eval_b = num_eval_b -1
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_alt=treatment*info_loc_gps_altitude_old
label var treat_alt "Treatment x altitude"
gen treat_conflict = treatment*ACLEDfatalities_x_proxa300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_bank = treatment*lg_bank_dist
label var treat_bank "Treatment x distance to any bank"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient death_x_prox_a300 treat_conflict 

global base_controls edu_b treat_edu num_eval_b treat_num lit_eval_b treat_lit

***Socioeconomic Survey outcomes ******************************************************************
clear matrix
mat Z = J(5,12,.)
mat starsZ = J(5,12,0)

mat A = J(4,12,.)
mat C = J(1,4,.)
mat D = J(1,4,.)


global n 1
local replace replace
local append
foreach var of varlist employment_index consumption_index savings_debt_index bizskills_index {
		
	
	eststo: xi: ivreg2 `var' lg_kcb_dist $geo_controls  i.strata (treatment1 treatment2=treatment Z_treat_kcb) , cluster(boma_old) savefprefix(fstage_) first savefirst
		local b_t1 = _b[treatment1]
		display `b_t1'
		mat A[$n, 1] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatment1]/_se[treatment1])))
		display `p_t1'
		mat A[$n, 2] = `p_t1'
		local b_t2 = _b[treatment2]
		display `b_t2'
		mat A[$n, 4] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment2]/_se[treatment2])))
		display `p_t2'
		mat A[$n, 5] = `p_t2'
		local obs = `e(N)'
		mat C[1,1] = `obs'
		local fstat = `e(rkf)'
		mat D[1,1] = `fstat'
			
			preserve
			parmest, norestore
			keep if parm=="treatment1" | parm=="treatment2"
			gen family="socioeconomic_controls_t1" if parm=="treatment1"
			replace family="socioeconomic_controls_t2" if parm=="treatment2"
			replace parm="`var'_t1" if parm=="treatment1"
			replace parm="`var'_t2" if parm=="treatment2"
			gen num=$n
			save "$gsdRawTemp\qvalue_ate_con_$n", replace
			restore
	eststo: xi: ivreg2 `var' lg_kcb_dist  $geo_controls $base_controls i.strata (treatment1 treatment2=treatment Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
		local b_t1 = _b[treatment1]
		display `b_t1'
		mat A[$n, 7] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatment1]/_se[treatment1])))
		display `p_t1'
		mat A[$n, 8] = `p_t1'
		local b_t2 = _b[treatment2]
		display `b_t2'
		mat A[$n, 10] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment2]/_se[treatment2])))
		display `p_t2'
		mat A[$n, 11] = `p_t2'
		local obs = `e(N)'
		mat C[1,3] = `obs'
		local fstat = `e(rkf)'
		mat D[1,3] = `fstat'
			
			preserve
			parmest, norestore
			keep if parm=="treatment1" | parm=="treatment2"
			gen family="socioeconomic_controls_t1" if parm=="treatment1"
			replace family="socioeconomic_controls_t2" if parm=="treatment2"
			replace parm="`var'_t1" if parm=="treatment1"
			replace parm="`var'_t2" if parm=="treatment2"
			gen num=$n
			save "$gsdRawTemp\qvalue_ate_geocon_$n", replace
			restore
			
	local replace
	local append append
	global n=$n+1
}

		matrix stars = J(4,12,0)
		foreach k of numlist 1/4 {
		  matrix stars[`k',1] = (abs(A[`k',2]) < 0.1) + (abs(A[`k',2]) < 0.05) + (abs(A[`k',2]) < 0.01)
		  matrix stars[`k',4] = (abs(A[`k',5]) < 0.1) + (abs(A[`k',5]) < 0.05) + (abs(A[`k',5]) < 0.01)
		  matrix stars[`k',7] = (abs(A[`k',8]) < 0.1) + (abs(A[`k',8]) < 0.05) + (abs(A[`k',8]) < 0.01)
		  matrix stars[`k',10] = (abs(A[`k',11]) < 0.1) + (abs(A[`k',11]) < 0.05) + (abs(A[`k',11]) < 0.01)

		  }



preserve
do "${gsdDo}\4-1-1-AdjustedPValues.do"
restore
	foreach num of numlist 1/4{
		mat A[`num',3] = B3[`num',1]
		}
	foreach num of numlist 1/4{
		mat A[`num',6] = B4[`num',1]
		}
	foreach num of numlist 1/4{
		mat A[`num',9] = B5[`num',1]
		}
	foreach num of numlist 1/4{
		mat A[`num',12] = B6[`num',1]
		}
			

		matrix rownames A = "Employment index" "Consumption index" "Savings index" "Business skills index"
		frmttable , statmat(A) replace sdec(3)  substat(2) plain annotate(stars) asymbol(*,**,***)  /// 
		title (Table H.2 – 2 instruments local average treatment effects of ex post treatments on main outcomes) ///
		ctitles(" ",  "\multicolumn{2}{c}{geo controls}", "\multicolumn{2}{c}{baseline + geography controls}" \ " ", ///
		 "``Training, no grant''" , "``Training and grant''", "``Training, no grant''" , "``Training and grant''") 
		frmttable , statmat(C) replace sdec(0) append rtitles("Observations")
		frmttable using "$gsdTables/Table7.a.tex", statmat(D) replay replace tex sdec(3) append rtitles("F-stat") plain
	
***Psychological and Behavioral Survey outcomes ******************************************************************
clear matrix
mat A = J(5,12,.)
mat C = J(1,4,.)
mat D = J(1,4,.)
mat Z = J(6,12,.)
mat starsZ = J(6,12,0)


global n 1
global m 1
local replace replace
local append
foreach var of varlist psycho_index risk_index trust_index crime_index migration_index {
	
	*LATE - 2 instruments
	
	eststo: xi: ivreg2 `var' lg_kcb_dist $geo_controls  i.strata (treatment1 treatment2=treatment Z_treat_kcb) , cluster(boma_old) savefprefix(fstage_) first savefirst
		local b_t1 = _b[treatment1]
		display `b_t1'
		mat A[$m, 1] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatment1]/_se[treatment1])))
		display `p_t1'
		mat A[$m, 2] = `p_t1'
		local b_t2 = _b[treatment2]
		display `b_t2'
		mat A[$m, 4] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment2]/_se[treatment2])))
		display `p_t2'
		mat A[$m, 5] = `p_t2'
		local obs = `e(N)'
		mat C[1,1] = `obs'
		local fstat = `e(rkf)'
		mat D[1,1] = `fstat'

			preserve
			parmest, norestore
			keep if parm=="treatment1" | parm=="treatment2"
			gen family="behavioral_controls_t1" if parm=="treatment1"
			replace family="behavioral_controls_t2" if parm=="treatment2"
			replace parm="`var'_t1" if parm=="treatment1"
			replace parm="`var'_t2" if parm=="treatment2"
			gen num=$n
			save "$gsdRawTemp\qvalue_ate_con_$n", replace
			restore
	eststo: xi: ivreg2 `var' lg_kcb_dist $base_controls $geo_controls i.strata (treatment1 treatment2=treatment Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
		local b_t1 = _b[treatment1]
		display `b_t1'
		mat A[$m, 7] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatment1]/_se[treatment1])))
		display `p_t1'
		mat A[$m, 8] = `p_t1'
		local b_t2 = _b[treatment2]
		display `b_t2'
		mat A[$m, 10] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment2]/_se[treatment2])))
		display `p_t2'
		mat A[$m, 11] = `p_t2'
		local obs = `e(N)'
		mat C[1,3] = `obs'
		local fstat = `e(rkf)'
		mat D[1,3] = `fstat'

			preserve
			parmest, norestore
			keep if parm=="treatment1" | parm=="treatment2"
			gen family="behavioral_controls_t1" if parm=="treatment1"
			replace family="behavioral_controls_t2" if parm=="treatment2"
			replace parm="`var'_t1" if parm=="treatment1"
			replace parm="`var'_t2" if parm=="treatment2"
			gen num=$n
			save "$gsdRawTemp\qvalue_ate_geocon_$n", replace
			restore
			
	
	local replace
	local append append
	global n=$n+1
	global m=$m+1
}

		matrix stars = J(5,12,0)
		foreach k of numlist 1/5 {
		  matrix stars[`k',1] = (abs(A[`k',2]) < 0.1) + (abs(A[`k',2]) < 0.05) + (abs(A[`k',2]) < 0.01)
		  matrix stars[`k',4] = (abs(A[`k',5]) < 0.1) + (abs(A[`k',5]) < 0.05) + (abs(A[`k',5]) < 0.01)
		  matrix stars[`k',7] = (abs(A[`k',8]) < 0.1) + (abs(A[`k',8]) < 0.05) + (abs(A[`k',8]) < 0.01)
		  matrix stars[`k',10] = (abs(A[`k',11]) < 0.1) + (abs(A[`k',11]) < 0.05) + (abs(A[`k',11]) < 0.01)

		  }

	
preserve
do "${gsdDo}\4-1-2-AdjustedPValuesPsy.do"
restore


	foreach num of numlist 1/5{
		mat A[`num',3] = B3[`num',1]
		}
	foreach num of numlist 1/5{
		mat A[`num',6] = B4[`num',1]
		}
	foreach num of numlist 1/5{
		mat A[`num',9] = B5[`num',1]
		}
	foreach num of numlist 1/5{
		mat A[`num',12] = B6[`num',1]
		}
	
		matrix rownames A = "Psychological wellbeing index" "Risk index" "Trust index" "Crime and violence index" ///
							"Migration index" 
		frmttable , statmat(A) replace sdec(3)  substat(2) plain annotate(stars) asymbol(*,**,***) /// 
		title (Table H.2 – 2 instruments local average treatment effects of ex post treatments on main outcomes) ///
		ctitles(" ",  "\multicolumn{2}{c}{controls}", "\multicolumn{2}{c}{controls + geography controls}" \ " ", ///
		 "``Training, no grant''" , "``Training and grant''", "``Training, no grant''" , "``Training and grant''") 
		frmttable , statmat(C) replace sdec(0) append rtitles("Observations")
		frmttable using "$gsdTables/Table7.b.tex", statmat(D) replay replace tex sdec(3) append rtitles("F-stat") plain

	